package com.zsc.shixun.controller;


import com.zsc.shixun.common.CommonResult;
import com.zsc.shixun.common.ResultUtil;
import com.zsc.shixun.model.type.TypeCreateVO;
import com.zsc.shixun.model.type.TypeUpdateVO;
import com.zsc.shixun.service.IArticleCategoriesService;
import com.zsc.shixun.utils.LoginMessageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 分类管理 前端控制器
 * </p>
 *
 * @author ZWYZY
 * @since 2020-06-06
 */
@Controller
@RequestMapping("/type")
@Validated
public class TypeController {
    private static final Logger logger = LoggerFactory.getLogger(TypeController.class);
    @Autowired
    private IArticleCategoriesService articleCategoriesService;
    @Autowired
    private LoginMessageUtils loginMessageUtils;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        request.setAttribute("articleVO", articleCategoriesService.getById(loginMessageUtils.getMessage().getLoginUser().getId()));
        return "admin/type/index";
    }

    /**
     * 获取分类列表
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    @Secured(value = "ROLE_type_select")
    public CommonResult getData() {
        return ResultUtil.success(articleCategoriesService.getById(loginMessageUtils.getMessage().getLoginUser().getId()));
    }

    /**
     * 修改分类类型
     *
     * @param typeUpdateVO
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @Secured(value = "ROLE_type_update")
    public CommonResult update(@Validated @ModelAttribute TypeUpdateVO typeUpdateVO) {
        articleCategoriesService.update(typeUpdateVO);
        return ResultUtil.success("修改分类类型成功哦，亲");
    }

    /**
     * 删除分类类型
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    @Secured(value = "ROLE_type_delete")
    public CommonResult delete(@NotNull(message = "id不能为空") @RequestParam(required = false) Long id) {
        articleCategoriesService.delete(id, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("删除分类类型成功哦，亲");
    }

    /**
     * 新增分类类型
     *
     * @param typeCreateVO
     * @return
     */
    @PostMapping("/insert")
    @ResponseBody
    @Secured(value = "ROLE_type_insert")
    public CommonResult insert(@Validated @ModelAttribute TypeCreateVO typeCreateVO) {
        articleCategoriesService.insert(typeCreateVO, loginMessageUtils.getMessage().getLoginUser().getId());
        return ResultUtil.success("创建文章分类类型成功哦，亲");
    }
}
